1 using UnityEngine;
2 using
System.Collections;
3
4 namespace
Completed
5 {
6     
//Enemy inherits from MovingObject, our base class for objects that can move, Player also inherits from this.
7     
public class Enemy : MovingObject
8     {
9         
public int playerDamage; //The amount of food points to subtract from the player when attacking.
10         
public AudioClip attackSound1; //First of two audio clips to play when attacking the player.
11         
public AudioClip attackSound2; //Second of two audio clips to play when attacking the player.
12         
13         
14         
private Animator animator; //Variable of type Animator to store a reference to the enemy's Animator component.
15         
private Transform target; //Transform to attempt to move toward each turn.
16         
private bool skipMove; //Boolean to determine whether or not enemy should skip a turn or move this turn.
17         
18         
19         
//Start overrides the virtual Start function of the base class.
20         
protected override void Start ()
21         {
22             
//Register this enemy with our instance of GameManager by adding it to a list of Enemy objects.
23             
//This allows the GameManager to issue movement commands.
24             GameManager.instance.AddEnemyToList (
this);
25             
26             
//Get and store a reference to the attached Animator component.
27             animator = GetComponent<Animator> ();
28             
29             
//Find the Player GameObject using it's tag and store a reference to its transform component.
30             target = GameObject.FindGameObjectWithTag (
"Player").transform;
31             
32             
//Call the start function of our base class MovingObject.
33             
base.Start ();
34         }
35         
36         
37         
//Override the AttemptMove function of MovingObject to include functionality needed for Enemy to skip turns.
38         
//See comments in MovingObject for more on how base AttemptMove function works.
39         
protected override void AttemptMove <T> (int xDir, int yDir)
40         {
41             
//Check if skipMove is true, if so set it to false and skip this turn.
42             
if(skipMove)
43             {
44                 skipMove =
false;
45                 
return;
46                 
47             }
48             
49             
//Call the AttemptMove function from MovingObject.
50             
base.AttemptMove <T> (xDir, yDir);
51             
52             
//Now that Enemy has moved, set skipMove to true to skip next move.
53             skipMove =
true;
54         }
55         
56         
57         
//MoveEnemy is called by the GameManger each turn to tell each Enemy to try to move towards the player.
58         
public void MoveEnemy ()
59         {
60             
//Declare variables for X and Y axis move directions, these range from -1 to 1.
61             
//These values allow us to choose between the cardinal directions: up, down, left and right.
62             
int xDir = 0;
63             
int yDir = 0;
64             
65             
//If the difference in positions is approximately zero (Epsilon) do the following:
66             
if(Mathf.Abs (target.position.x - transform.position.x) < float.Epsilon)
67                 
68                 
//If the y coordinate of the target's (player) position is greater than the y coordinate of this enemy's position set y direction 1 (to move up). If not, set it to -1 (to move down).
69                 yDir = target.position.y > transform.position.y ?
1 : -1;
70             
71             
//If the difference in positions is not approximately zero (Epsilon) do the following:
72             
else
73                 
//Check if target x position is greater than enemy's x position, if so set x direction to 1 (move right), if not set to -1 (move left).
74                 xDir = target.position.x > transform.position.x ?
1 : -1;
75             
76             
//Call the AttemptMove function and pass in the generic parameter Player, because Enemy is moving and expecting to potentially encounter a Player
77             AttemptMove <Player> (xDir, yDir);
78         }
79         
80         
81         
//OnCantMove is called if Enemy attempts to move into a space occupied by a Player, it overrides the OnCantMove function of MovingObject
82         
//and takes a generic parameter T which we use to pass in the component we expect to encounter, in this case Player
83         
protected override void OnCantMove <T> (T component)
84         {
85             
//Declare hitPlayer and set it to equal the encountered component.
86             Player hitPlayer = component
as Player;
87             
88             
//Call the LoseFood function of hitPlayer passing it playerDamage, the amount of foodpoints to be subtracted.
89             hitPlayer.LoseFood (playerDamage);
90             
91             
//Set the attack trigger of animator to trigger Enemy attack animation.
92             animator.SetTrigger (
"enemyAttack");
93             
94             
//Call the RandomizeSfx function of SoundManager passing in the two audio clips to choose randomly between.
95             SoundManager.instance.RandomizeSfx (attackSound1, attackSound2);
96         }
97     }
98 }


Enemy inherits from MovingObject, our base class for objects that can move, Player also inherits from this.

public int playerDamage; The amount of food points to subtract from the player when attacking.

public AudioClip attackSound1; First of two audio clips to play when attacking the player.

public AudioClip attackSound2; Second of two audio clips to play when attacking the player.

private Animator animator; Variable of type Animator to store a reference to the enemy's Animator component.

private Transform target; Transform to attempt to move toward each turn.

private bool skipMove; Boolean to determine whether or not enemy should skip a turn or move this turn.

Start overrides the virtual Start function of the base class.

Register this enemy with our instance of GameManager by adding it to a list of Enemy objects.

This allows the GameManager to issue movement commands.

Get and store a reference to the attached Animator component.

Find the Player GameObject using it's tag and store a reference to its transform component.

Call the start function of our base class MovingObject.

Override the AttemptMove function of MovingObject to include functionality needed for Enemy to skip turns.

See comments in MovingObject for more on how base AttemptMove function works.

Check if skipMove is true, if so set it to false and skip this turn.

Call the AttemptMove function from MovingObject.

Now that Enemy has moved, set skipMove to true to skip next move.

MoveEnemy is called by the GameManger each turn to tell each Enemy to try to move towards the player.

Declare variables for X and Y axis move directions, these range from -1 to 1.

These values allow us to choose between the cardinal directions: up, down, left and right.

If the difference in positions is approximately zero (Epsilon) do the following:

If the y coordinate of the target's (player) position is greater than the y coordinate of this enemy's position set y direction 1 (to move up). If not, set it to -1 (to move down).

If the difference in positions is not approximately zero (Epsilon) do the following:

Check if target x position is greater than enemy's x position, if so set x direction to 1 (move right), if not set to -1 (move left).

Call the AttemptMove function and pass in the generic parameter Player, because Enemy is moving and expecting to potentially encounter a Player

OnCantMove is called if Enemy attempts to move into a space occupied by a Player, it overrides the OnCantMove function of MovingObject

and takes a generic parameter T which we use to pass in the component we expect to encounter, in this case Player

Declare hitPlayer and set it to equal the encountered component.

Call the LoseFood function of hitPlayer passing it playerDamage, the amount of foodpoints to be subtracted.

Set the attack trigger of animator to trigger Enemy attack animation.

Call the RandomizeSfx function of SoundManager passing in the two audio clips to choose randomly between.




Trò chơi giống như Rogue 2D sử dụng Unity 28.450 lượt xem

Gõ tìm kiếm nhanh...